package com.lysj.bhp.api.controller;

import com.lysj.bhp.api.base.ApiContent;
import com.lysj.bhp.borrowing.domain.BorrowingRate;
import com.lysj.bhp.borrowing.domain.BorrowingRecord;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.wallet.domain.BhpcWallet;
import com.lysj.bhp.wallet.domain.BitCoinWallet;
import com.lysj.bhp.wallet.domain.EthereumWallet;
import com.lysj.bhp.wallet.vo.PledgeWalletVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
/**
 * @Author:ZhangWenJian
 * @Create:2018/8/14--9:01
 * @Description:
 */
@RestController
@RequestMapping("/api/v2/user")
@Api(value = "ApiBorrowingController", tags = "借贷模块")
public class ApiBorrowingController extends ApiContent {
    //查询理财记录
    @ApiOperation(value = "借贷利率", notes = "查询条件 3个参数 ：一.Integer rateType 利率类型：1.天，2.月 二. Integer repaymentWay.还款方式: 1:到期还本付息,2:等额本息,3:先息后本   返回规则：有查询到就返回该利率所有信息，未查询到则返回null")
    @GetMapping("/borrowing_rate")
    public Resp borrowingRate(Integer rateType,Integer repaymentWay) {
        /*查找 2个参数条件下，状态为正常的利率类型*/
        BorrowingRate borrowingRate = borrowingContent.borrowingRateRepository.findByRateTypeAndRepaymentWayAndIsExist(rateType,repaymentWay,1);
        if(borrowingRate == null){
            borrowingRate = new BorrowingRate();
            borrowingRate.setBorrowingRate(new BigDecimal(30));
        }
        return new Resp(borrowingRate);
    }
    //添加申请借贷记录
    @ApiOperation(value = "申请借贷")
    @PostMapping("/apply_for_borrowing")
    public Resp applyForBorrowing(BorrowingRecord model,String password){
        return borrowingContent.borrowingMoneyService.applyForBorrowing(model,password);
    }
    //查找资产够1w的用户
    @ApiOperation(value = "钱包资产>1W", notes = "查询条件无")
    @GetMapping("/pledge_wallet")
    public Resp walletProperty(){
        Map<String, Object> map = new HashMap<>();
        PledgeWalletVo bitcoinWallteVo = new PledgeWalletVo();
        PledgeWalletVo ethereumWalletVo = new PledgeWalletVo();
        PledgeWalletVo bhpcWalletVo = new PledgeWalletVo();
        List<PledgeWalletVo> walletVos = new ArrayList<>();
        //查询bhpc钱包
        String maxValue = "";
        BigDecimal maxMoney = BigDecimal.valueOf(0);
        // 10:btc,20:eth,100:bhpc,
        //比特币
        BitCoinWallet bitCoinWallet = bitcoinWalletRepository.findByUserId(currentUser().getUserId());
        if(bitCoinWallet != null){
            bitcoinWallteVo.setName("BTC");
            bitcoinWallteVo.setChineseName("比特币");
            bitcoinWallteVo.setType(10);
            bitcoinWallteVo.setAvailBalance(bitCoinWallet.getAvailBalance().setScale(6,BigDecimal.ROUND_HALF_UP));
            BigDecimal cnyAvailBalance = bitcoinWallteVo.getAvailBalance().multiply(exchangeRateService.bitcoinToCnyRate()).setScale(4,BigDecimal.ROUND_HALF_UP);
            bitcoinWallteVo.setCnyAvailBalance(cnyAvailBalance);
            if(cnyAvailBalance.compareTo(new BigDecimal("10000"))>=1){
                walletVos.add(bitcoinWallteVo);
                if(cnyAvailBalance.compareTo(maxMoney)>=1){
                    maxMoney = cnyAvailBalance;
                    maxValue = "比特币";
                }

            }
        }
        //以太坊
        EthereumWallet ethereumWallet = ethereumWalletRepository.findByUserId(currentUser().getUserId());
        if(ethereumWallet != null){
            ethereumWalletVo.setName("ETH");
            ethereumWalletVo.setChineseName("以太坊");
            ethereumWalletVo.setType(20);
            ethereumWalletVo.setAvailBalance(ethereumWallet.getAvailBalance().setScale(6,BigDecimal.ROUND_HALF_UP));
            BigDecimal cnyAvailBalance = ethereumWalletVo.getAvailBalance().multiply(exchangeRateService.ethToCnyRate()).setScale(4,BigDecimal.ROUND_HALF_UP);
            ethereumWalletVo.setCnyAvailBalance(cnyAvailBalance);
            if(cnyAvailBalance.compareTo(new BigDecimal("10000"))>=1){
                walletVos.add(ethereumWalletVo);
                if(cnyAvailBalance.compareTo(maxMoney)>=1){
                    maxMoney = cnyAvailBalance;
                    maxValue = "以太坊";
                }
            }
        }
        //bhpc
        BhpcWallet bhpcWallet =  bhpcWalletRepository.findByUserId(currentUser().getUserId());
        if(bhpcWallet != null){
            bhpcWalletVo.setName("BHPC");
            bhpcWalletVo.setChineseName("算力现金");
            bhpcWalletVo.setType(100);
            bhpcWalletVo.setAvailBalance(bhpcWallet.getAvailBalance().setScale(6,BigDecimal.ROUND_HALF_UP));
            BigDecimal cnyAvailBalance = bhpcWalletVo.getAvailBalance().multiply(exchangeRateService.bhpcToCnyRate()).setScale(4,BigDecimal.ROUND_HALF_UP);
            bhpcWalletVo.setCnyAvailBalance(cnyAvailBalance);
            if(cnyAvailBalance.compareTo(new BigDecimal("10000"))>=1){
                walletVos.add(bhpcWalletVo);
                if(cnyAvailBalance.compareTo(maxMoney)>=1){
                    maxValue = "算力现金";
                }
            }
        }
        //构建返回Map
        map.put("walletVos",walletVos);
        map.put("maxValue",maxValue);
        return new Resp(map);
    }
    //查询所抵押资产最大额度
    @ApiOperation(value = "额度范围", notes = "查询条件 2个参数：①.抵押钱包类型：Integer pledgeType 10:btc,20:eth,100:bhpc ②.借款类型：Integer borrowType 10:btc,20:eth,100:bhpc")
    @GetMapping("/limit")
    public Resp limit(Integer pledgeType,Integer borrowType){
        Map<String, Object> map = new HashMap<>();
        BigDecimal pledgeAvailBalance = new BigDecimal(BigInteger.ZERO);
        BigDecimal borrowAvailBalance = new BigDecimal(BigInteger.ZERO);
        switch (pledgeType){
            case 10:
                BitCoinWallet bitCoinWallet = bitcoinWalletRepository.findByUserId(currentUser().getUserId());
                pledgeAvailBalance = bitCoinWallet.getAvailBalance().multiply(exchangeRateService.bitcoinToCnyRate());
                break;
            case 20:
                EthereumWallet ethereumWallet = ethereumWalletRepository.findByUserId(currentUser().getUserId());
                pledgeAvailBalance = ethereumWallet.getAvailBalance().multiply(exchangeRateService.ethToCnyRate());
                break;
            case 100:
                BhpcWallet bhpcWallet =  bhpcWalletRepository.findByUserId(currentUser().getUserId());
                pledgeAvailBalance = bhpcWallet.getAvailBalance().multiply(exchangeRateService.bhpcToCnyRate());
                break;
        }
        switch (borrowType){
            case 10:
                borrowAvailBalance = pledgeAvailBalance.divide(exchangeRateService.bitcoinToCnyRate(),8,BigDecimal.ROUND_HALF_UP);
                break;
            case 20:
                borrowAvailBalance = pledgeAvailBalance.divide(exchangeRateService.ethToCnyRate(),8,BigDecimal.ROUND_HALF_UP);
                break;
            case 100:
                borrowAvailBalance = pledgeAvailBalance.divide(exchangeRateService.bhpcToCnyRate(),8,BigDecimal.ROUND_HALF_UP);
                break;
        }
        map.put("minLimit",borrowAvailBalance.multiply(BigDecimal.valueOf(0.1)).setScale(6,BigDecimal.ROUND_HALF_UP));
        map.put("maxLimit",borrowAvailBalance.multiply(BigDecimal.valueOf(0.5)).setScale(6,BigDecimal.ROUND_HALF_UP));
        return new Resp(map);
    }


    //撤销
    @ApiOperation(value = "撤销操作", notes = "查询条件 1个参数：单号Id：Long borrowingRecordId ")
    @PostMapping("/borrowing_repeal")
    public Resp borrowingRepeal(Long borrowingRecordId){
        return borrowingContent.borrowingRecordService.borrowingRecordRevocation(borrowingRecordId);
    }

    @ApiOperation(value = "预估抵押金额", notes = "查询条件 3个参数：所借金额类型：Integer borrowingType,所借金额数目：BigDecimal borrowingMoney,抵押金额类型：Integer mortgageType ")
    @PostMapping("/estimate_pledge_money")
    public Resp estimatePledgeMoney(Integer borrowingType,BigDecimal borrowingMoney,Integer mortgageType){
        BigDecimal borrowingCnyMoney = new BigDecimal(BigInteger.ZERO);
        BigDecimal mortgageMoney = new BigDecimal(BigInteger.ZERO);
        BigDecimal mortgageCnyMoney = new BigDecimal(BigInteger.ZERO);
        borrowingCnyMoney = exchangeRateService.qtumMoneyToCny(borrowingType,borrowingMoney);
        switch (mortgageType){
            case 10:
                mortgageMoney = (borrowingCnyMoney.divide(exchangeRateService.bitcoinToCnyRate(),6,BigDecimal.ROUND_HALF_UP)).multiply(new BigDecimal(2));
                mortgageCnyMoney = mortgageMoney.multiply(exchangeRateService.bitcoinToCnyRate()).setScale(2,BigDecimal.ROUND_HALF_UP);
                break;
            case 20:
                mortgageMoney = (borrowingCnyMoney.divide(exchangeRateService.ethToCnyRate(),6,BigDecimal.ROUND_HALF_UP)).multiply(new BigDecimal(2));
                mortgageCnyMoney = mortgageMoney.multiply(exchangeRateService.ethToCnyRate()).setScale(2,BigDecimal.ROUND_HALF_UP);
                break;
            case 100:
                mortgageMoney = (borrowingCnyMoney.divide(exchangeRateService.bhpcToCnyRate(),6,BigDecimal.ROUND_HALF_UP)).multiply(new BigDecimal(2));
                mortgageCnyMoney = mortgageMoney.multiply(exchangeRateService.bhpcToCnyRate()).setScale(2,BigDecimal.ROUND_HALF_UP);
                break;
        }
        Map<String,Object> hintMoney = new HashMap<>();
        hintMoney.put("mortgageMoney",mortgageMoney);
        hintMoney.put("borrowingCnyMoney",borrowingCnyMoney.setScale(2,BigDecimal.ROUND_HALF_UP));
        hintMoney.put("mortgageCnyMoney",mortgageCnyMoney.setScale(2,BigDecimal.ROUND_HALF_UP));
        return new Resp(hintMoney);
    }
}
